package mil.af.amc.adpetracker.web.helpers;

import mil.af.amc.adpetracker.model.Account;
import mil.af.amc.adpetracker.service.AccountService;
import mil.af.amc.radtaglibrary.tags.PageContextAwareBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.jsp.PageContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * View Helper for Account objects
 *
 * @author TSgt Jason Ferguson
 */
@Service("accountViewHelper")
public class AccountViewHelper implements PageContextAwareBean {

    private PageContext pageContext;

    @Autowired
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    private AccountService accountService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    public Account getAccount() {

        int accountId = Integer.parseInt(pageContext.getRequest().getParameter("accountId"));

        return accountService.getById(accountId);

    }

    public List<Account> getAll() {

        List<Account> results = accountService.getAll();
        Set<Account> tempResultSet = new TreeSet<Account>(results); // Big hammer method to eliminate dupes

        return new ArrayList<Account>(tempResultSet);

//        // FUGLY!
//        Collections.sort(results, new Comparator<Account>() {
//            public int compare(Account o1, Account o2) {
//                return o1.getDisplayName().compareTo(o2.getDisplayName());
//            }
//        });
//
//        return results;

    }
}
